package com.simple.accounting.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.simple.accounting.R;
import com.simple.accounting.db.UserDBManager;
import com.simple.accounting.db.UserInfoBean;
import com.simple.accounting.utils.ToastUtils;

public class ChangePwdActivity extends BaseActivity {

    private EditText edtOldPwd;
    private EditText edtNewPwd;
    private EditText edtCfNewPwd;
    private Button btnCf;

    private SharedPreferences sp;
    public UserInfoBean mUserInfoBean = new UserInfoBean();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);

        initView();
        sp = getSharedPreferences("menu",MODE_PRIVATE);
    }

    private void initView() {
        edtOldPwd = findViewById(R.id.edt_old_pwd);
        edtNewPwd = findViewById(R.id.edt_new_pwd);
        edtCfNewPwd = findViewById(R.id.edt_confirm_new_pwd);
        btnCf = findViewById(R.id.confirm);
    }

    @SuppressLint("NonConstantResourceId")
    public void onClick(View view){
        switch (view.getId()){
            case R.id.changepwd_back:
                finish();
                break;
            case R.id.confirm:
                checkPwd();
                break;
        }
    }

    /*核验修改密码*/
    private void checkPwd(){
        String username = sp.getString("username", "");
        Log.i("TAG", "checkPwd: "+username);
        String pwd = sp.getString("pw","");//从SharedPreferences中拿到当前登录用户的密码
        String oldPwd = edtOldPwd.getText().toString().trim();
        String newPwd = edtNewPwd.getText().toString().trim();
        String newCfPwd = edtCfNewPwd.getText().toString().trim();
        if (!pwd.equals(oldPwd)){
            ToastUtils.show(this,"输入的旧密码不正确！",ToastUtils.TYPE_ERROR);
        }else {
            if (newPwd.length()>12){
                ToastUtils.show(this,"输入的新密码长度不能大于12！",ToastUtils.TYPE_ERROR);
            }else if (newPwd.length()<6){
                ToastUtils.show(this,"输入的新密码长度不能小于6！",ToastUtils.TYPE_ERROR);
            }else if (newPwd.equals(pwd)){
                ToastUtils.show(this,"输入的新密码不能与原密码相同！",ToastUtils.TYPE_ERROR);
            }else if (!newPwd.equals(newCfPwd)){
                ToastUtils.show(this,"确认新密码与输入的新密码不同",ToastUtils.TYPE_ERROR);
            }else if (newPwd.equals(newCfPwd)){
                mUserInfoBean.setPassword(newPwd);
                mUserInfoBean.setUsername(username);
                try {
                    int i = UserDBManager.updatePwdToUserTB(mUserInfoBean);//执行更新密码的操作
                    if (i>0){
                        ToastUtils.show(this,"密码更新成功！",ToastUtils.TYPE_SUCCESS);
                        /*跳转到设置界面*/
                        Intent intent = new Intent(ChangePwdActivity.this,SettingActivity.class);
                        startActivity(intent);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }
    }
}
